package com.meihua.baoleyi.pay;

import android.support.v4.app.FragmentActivity;

import com.meihua.baoleyi.bean.WXPayBean;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.ToastUtil;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by jzm on 2017/11/2.
 * 微信支付
 */

public class WxPay {

    public static void wxPay(FragmentActivity mContext, String json) {
        WXPayBean wx = NetHelper.getBeanForJson(WXPayBean.class, json);
        if (wx == null) {
            return;
        }
        try {
            if (wx.getCode().equals("1")) {
                //调用微信支付
                IWXAPI mWxApi = WXAPIFactory.createWXAPI(mContext, wx.getData().getAppid(), true);
                mWxApi.registerApp(wx.getData().getAppid());
                PayReq req = new PayReq();
                req.appId = wx.getData().getAppid();// 微信开放平台审核通过的应用APPID
                req.partnerId = wx.getData().getMch_id();// 微信支付分配的商户号
                req.prepayId = wx.getData().getPrepay_id();// 预支付订单号，app服务器调用“统一下单”接口获取
                req.packageValue = "Sign=WXPay";// 固定值Sign=WXPay，可以直接写死，服务器返回的也是这个固定值
                req.nonceStr = wx.getData().getNonce_str();// 随机字符串，不长于32位，服务器小哥会给咱生成
                req.timeStamp = wx.getData().getTimestamp() + "";// 时间戳，app服务器小哥给出
                req.sign = wx.getData().getSign();// 签名，服务器小哥给出，他会根据：https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3指导得到这个
                mWxApi.sendReq(req);
            } else {
                ToastUtil.show(mContext, wx.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
